package com.matt.controller.pad;

import com.dotflat.util.excelUtil.StringUtils;
import com.matt.commons.dto.sso.StaffDTO;
import com.matt.commons.model.StatusResponse;
import com.matt.commons.page.PagerQuery;
import com.matt.domain.TeachOperationLog;
import com.matt.foundation.model.MemVerificationCache;
import com.matt.foundation.query.OperationLogQuery;
import com.matt.foundation.utils.annotation.RequiredPermissions;
import com.matt.service.StaffActionLogService;
import com.matt.service.TeachOperationLogService;
import com.matt.service.remoteCall.DTO.RoleDTO;
import com.matt.service.remoteCall.SsoCallService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

import static com.matt.foundation.constant.PermissionConstant.TEACH_ROLE_LIST;
import static com.matt.foundation.constant.PermissionConstant.TEACH_STAFF_EDIT;

/**
 * *******************************
 * Created by Ron.
 * form com.matt
 * on 2017/11/13
 * *******************************
 */
@RestController
@RequestMapping("/pad/staff")
@Api(tags = "员工n权限")
public class PadStaffController {

    @Autowired
    private SsoCallService ssoCallService;

    @Autowired
    private MemVerificationCache cache;

    @Autowired
    private TeachOperationLogService teachOperationLogService;

    @Autowired
    private StaffActionLogService staffActionLogService;

    @ApiOperation(value = "获取员工基础信息",httpMethod = "POST")
    @RequestMapping("/base_info")
    public StatusResponse getBaseInfo(String staffId) {
        StaffDTO staff = ssoCallService.getStaffDTO(staffId);
        System.out.println("====realName=====:"+staff.getRealName());
        return StatusResponse.success(staff);
    }

    @ApiOperation(value = "获取员工基础信息",httpMethod = "POST")
    @RequestMapping("/base_info2")
    public StatusResponse base_info2(String staffId) {
        Map staffDTO2 = ssoCallService.getStaffDTO2(staffId);
        System.out.println("====realName=====:"+staffDTO2.get("RealName"));
        return StatusResponse.success(staffDTO2);
    }

    @ApiOperation(value = "员工日志列表",httpMethod = "POST")
    @RequestMapping("/operation_log")
    public StatusResponse operationLog(OperationLogQuery query) {
        Page<TeachOperationLog> logPage = teachOperationLogService.getMyLogByCondition(query);
        return StatusResponse.success(logPage);
    }

    @ApiOperation(value = "修改员工基础信息",httpMethod = "POST")
    @RequestMapping("/modify_base_info")
    public StatusResponse modifyBaseInfo(@RequestHeader String ticket, StaffDTO staff){
        return StatusResponse.success(ssoCallService.updateStaff(ticket, staff));
    }

    @ApiOperation(value = "员工离职",httpMethod = "POST")
    @RequestMapping("/quit")
    public StatusResponse staffQuit(@RequestHeader String ticket, String staffId){
        return StatusResponse.success(ssoCallService.quitStaff(ticket, staffId));
    }

    @ApiOperation(value = "员工入职",httpMethod = "POST")
    @RequestMapping("/entry")
    public StatusResponse staffEntry(@RequestHeader String ticket, String staffId){
        return StatusResponse.success(ssoCallService.staffEntry(ticket, staffId));
    }

    @ApiOperation(value = "获取员工角色列表")
    @GetMapping("/staff_roles")
    public StatusResponse staffRoles(@RequestHeader String ticket, String staffId){
        return StatusResponse.success(ssoCallService.getStaffRoleList(ticket, staffId));
    }

    @ApiOperation(value = "重置员工角色列表")
    @PostMapping("/staff_roles")
    @RequiredPermissions(TEACH_STAFF_EDIT)
    public StatusResponse setStaffRoles(@RequestHeader String ticket, String staffId, String[] roleIds){
        ssoCallService.setStaffRoleList(ticket, staffId, roleIds);
        staffActionLogService.saveAreaRoles(ticket, staffId);
        return StatusResponse.success();
    }

    @ApiOperation(value = "园区角色列表")
    @GetMapping("/area_roles")
    @RequiredPermissions(TEACH_ROLE_LIST)
    public StatusResponse areaRoles(@RequestHeader String ticket) {
        return StatusResponse.success(ssoCallService.getAreaRoles(ticket, true));
    }

    @ApiOperation(value = "创建或修改园区角色")
    @PostMapping("/area_roles")
    @RequiredPermissions(TEACH_ROLE_LIST)
    public StatusResponse addAreaRoles(@RequestHeader String ticket, RoleDTO role) {
        RoleDTO roleDTO = ssoCallService.addAreaRole(ticket, role);
        staffActionLogService.save(ticket, "添加("+roleDTO.getRoleName()+")角色");
        return StatusResponse.success(roleDTO);
    }

    @ApiOperation(value = "移除园区角色")
    @DeleteMapping("/area_roles")
    @RequiredPermissions(TEACH_ROLE_LIST)
    public StatusResponse deleteAreaRoles(@RequestHeader String ticket, RoleDTO role) {
        ssoCallService.removeAreaRole(ticket, role);
        staffActionLogService.save(ticket, "移除("+role.getRoleName()+")角色");
        return StatusResponse.success();
    }

//    @ApiOperation(value = "WEB 园区所有老师, 需要权限（teach::staff::list）",httpMethod = "POST")
//    @RequestMapping("/area_staff")
//    public StatusResponse allByArea(@RequestHeader(RequestConstant.TICKET) String ticket, PagerQuery pagerQuery) {
//        return StatusResponse.success(ssoCallService.getAreaStaffList(ticket, null, PermissionConstant.TEACH_CLASSES_MYLIST, pagerQuery));
//    }

    @ApiOperation(value = "获取园区员工",httpMethod = "GET")
    @RequestMapping("/area_staff")
//    @RequiredPermissions(TEACH_STAFF_LIST)
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "header", name = "ticket", value = "票据", required = true, dataType = "string"),
            @ApiImplicitParam(paramType = "formData", name = "name", value = "名称模糊查找", dataType = "string"),
            @ApiImplicitParam(paramType = "formData", name = "roleId", value = "角色id过滤", dataType = "string")
    })
    public StatusResponse areaStaff(@RequestHeader String ticket, String name, String roleId, PagerQuery pagerQuery) {
        return StatusResponse.success(ssoCallService.getAreaStaffList(ticket, name, roleId, pagerQuery));
    }

    @ApiOperation(value = "获取平台对应资源列表")
    @GetMapping("/platform_menus")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "header", name = "ticket", value = "票据", required = true, dataType = "string"),
            @ApiImplicitParam(paramType = "query", name = "term", value = "term为0时 web端，   1时 app端,   2时小程序端", required = true, dataType = "int")
    })
    public StatusResponse getMenuByPlatform(@RequestHeader String ticket,@RequestHeader(required = false) String platformId){
        return StatusResponse.success(ssoCallService.getTermMenus(ticket, 1,platformId));
    }

    @ApiOperation(value = "获取角色对应资源列表")
    @GetMapping("/role_menus")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "header", name = "ticket", value = "票据", required = true, dataType = "string"),
            @ApiImplicitParam(paramType = "query", name = "roleId", value = "角色id", required = true, dataType = "string"),
            @ApiImplicitParam(paramType = "query", name = "term", value = "term为0时 web端，   1时 app端,   2时小程序端", required = true, dataType = "int")
    })
    public StatusResponse getMenuByRole(@RequestHeader String ticket, String roleId, int term,@RequestHeader(required = false) String platformId){
        return StatusResponse.success(ssoCallService.getRoleMenus(ticket, roleId, 0,platformId));
    }

    @ApiOperation(value = "设置角色对应资源列表")
    @PostMapping("/role_menus")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "header", name = "ticket", value = "票据", required = true, dataType = "string"),
            @ApiImplicitParam(paramType = "query", name = "roleId", value = "角色id", required = true, dataType = "string"),
            @ApiImplicitParam(paramType = "query", name = "term", value = "term为0时 web端，   1时 app端,   2时小程序端", required = true, dataType = "int")
    })
    public StatusResponse setMenuByRole(@RequestHeader String ticket, String roleId, String[] menuIds, int term,@RequestHeader(required = false) String platformId){
        return StatusResponse.success(ssoCallService.setRoleMenus(ticket, roleId, menuIds, term,platformId));
    }
}
